﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using QUANLYCHAMCONG.BL.Entities;
using Hopthoai;
using QUANLYCHAMCONG.BL.BUS;
using DgvFilterPopup;

namespace QUANLYCHAMCONG.PL.CMS.Maychamcong
{
    public partial class Khaibaomaychamcong : UserControl
    {

        public delegate void _Close();
        public event _Close Close;
        MAYCHAMCONG maychamcong;
        int flag = 0;
        frmHoidap f;
        public Khaibaomaychamcong()
        {
            InitializeComponent();
        }

        private void cbtenmin_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbtenmin.Checked == true)
                txttenmien.ReadOnly = false;
            else
                txttenmien.ReadOnly = true;
        }
        private void Event()
        {

            txtidmay.Focus();
            this.toolbar1.Add += new Toolbar._Add(toolbar1_Add);
            this.toolbar1.Edit += new Toolbar._Edit(toolbar1_Edit);
            this.toolbar1.Save += new Toolbar._Save(Save);
            this.toolbar1.Delete += new Toolbar._Delete(toolbar1_Delete);
            this.toolbar1.Close += new Toolbar._Close(toolbar1_Close);
            this.toolbar1.Refresh += new Toolbar._Refesh(Refresh);
            InitGrid();
            LoadData();
        }
        void Refresh()
        {
            RemoveCheckbox();
            this.toolbar1.SetSave();
            this.flag = 0;
            this.dgvdanhsachmay.ReadOnly = true;
            this.dgvdanhsachmay.AllowUserToAddRows = true;
            LoadData();
            this.Clear();
        }
        void toolbar1_Close()
        {
            if (this.Close != null)
                this.Close();
        }
        private int FindAviable()
        {
            foreach (DataGridViewColumn dr in this.dgvdanhsachmay.Columns)
            {
                if (dr is DataGridViewCheckBoxColumn)
                    return 1;
            }
            return 0;
        }
        private void RemoveCheckbox()
        {
            if (FindAviable() == 1)
            {
                this.dgvdanhsachmay.Columns.RemoveAt(0);
                this.dgvdanhsachmay.Controls.RemoveByKey(((CheckBox)this.dgvdanhsachmay.Controls.Find("checkboxHeader", true)[0]).Name);
                LoadData();
            }
        }
        private void AddCheckbox(DataTable dt)
        {
            if (FindAviable() == 0)
            {
                DataGridViewCheckBoxColumn cb = new DataGridViewCheckBoxColumn();
                cb.Width = 30;
                cb.FillWeight = 30;
                cb.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                this.dgvdanhsachmay.Columns.Insert(0, cb);
                Rectangle rect = this.dgvdanhsachmay.GetCellDisplayRectangle(0, -1, true);
                rect.X = rect.Location.X + cb.Width / 33;
                CheckBox checkboxHeader = new CheckBox();
                checkboxHeader.Name = "checkboxHeader";
                checkboxHeader.Size = new Size(18, 18);
                checkboxHeader.Location = rect.Location;
                checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);
                this.dgvdanhsachmay.Controls.Add(checkboxHeader);
                this.dgvdanhsachmay.Columns[0].Width = 50;
                this.dgvdanhsachmay.DataSource = dt;
            }
        }
        void checkboxHeader_CheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox)this.dgvdanhsachmay.Controls.Find("checkboxHeader", true)[0]).Checked == true)
            {
                for (int i = 0; i < this.dgvdanhsachmay.RowCount; i++)
                {
                    this.dgvdanhsachmay[0, i].Value = ((CheckBox)this.dgvdanhsachmay.Controls.Find("checkboxHeader", true)[0]).Checked;
                }
            }
            else
            {
                for (int i = 0; i < this.dgvdanhsachmay.RowCount; i++)
                {
                    this.dgvdanhsachmay[0, i].Value = false;
                }
            }
        }
        void toolbar1_Delete()
        {
            flag = 3;
            this.dgvdanhsachmay.ReadOnly = false;
            this.dgvdanhsachmay.AllowUserToAddRows = false;
            this.toolbar1.SetAdd_Edit_Delete();
            AddCheckbox(MAYCHAMCONGBUS.SelectAll());

        }
        void toolbar1_Edit()
        {
            flag = 2;
            Clear();
            this.dgvdanhsachmay.Focus();
            this.dgvdanhsachmay.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dgvdanhsachmay.Rows[0].Selected = true;
            this.dgvdanhsachmay.ReadOnly = false;
            this.dgvdanhsachmay.AllowUserToAddRows = false;
            this.toolbar1.SetAdd_Edit_Delete();
        }
        void toolbar1_Add()
        {
            flag = 1;
            Clear();
            CreateID();
            this.toolbar1.SetAdd_Edit_Delete();
            txtidmay.Focus();
            this.cmbloaikn.SelectedIndex = 0;
            SetIp();

        }
        void SetIp()
        {

            if (this.cmbloaimay.SelectedValue.ToString() != "" && this.cmbloaimay.SelectedIndex != -1)
            {
                if (this.cmbloaimay.SelectedValue.ToString() == "LM110521")
                {
                    txtport.Text = "4370";
                }
                if (this.cmbloaimay.SelectedValue.ToString() == "LM1105211")
                {
                    txtport.Text = "5005";
                }
            }
        }
        MAYCHAMCONG AddDuLieu(MAYCHAMCONG mcc)
        {
            if (this.cmbloaikn.Text == "TCP/IP")
            {
                mcc.IP = txtip.Text;
                mcc.PORT = txtport.Text;
                mcc.PASSWORD = txtpass.Text;
                mcc.COMPORT = 0;
                mcc.MATD = null;
            }
            else if (this.cmbloaikn.Text == "COM")
            {
                mcc.COMPORT = int.Parse(cmbcom.Text.ToString().Replace("COM", ""));
                mcc.MATD = cmbtocdo.SelectedValue.ToString();
                mcc.IP = null;
                mcc.PORT = null;
                mcc.PASSWORD = null;
            }
            else if (this.cmbloaikn.Text == "USB")
            {
                mcc.IP = "";
                mcc.PORT = "";
                mcc.PASSWORD = "";
                mcc.COMPORT = 0;
                mcc.MATD = null;
            }
            mcc.FIMWARE = txtfimwave.Text;
            mcc.IDMAY = int.Parse( txtidmay.Text);
            if (cbtenmin.Checked == true)
            {
                mcc.ISDOMAIN = true;
                mcc.DOMAIN = txttenmien.Text;
            }
            else
            {
                mcc.ISDOMAIN = false;
                mcc.DOMAIN = null;
            }
            mcc.MALKN = cmbloaikn.SelectedValue.ToString();
            mcc.MALM = cmbloaimay.SelectedValue.ToString();
            mcc.MAMCC = txtmamcc.Text;
            mcc.SeriNumber = null;
            mcc.SODANGKI = null;
            mcc.TENMAY = txttenmay.Text;
            mcc.TONGSOMATMA = 0;
            mcc.TONGSONHANVIEN = 0;
            mcc.TONGSOVANTAY = 0;
            return mcc;
        }
        void Save()
        {
            //khoi tao
           maychamcong=new MAYCHAMCONG();
            if (flag == 1)
            {
                if (MAYCHAMCONGBUS.Insert(AddDuLieu(maychamcong)) == true)
                {
                    f = new frmHoidap("Thêm Máy chấm công  thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                    f.ShowDialog();
                    LoadData();
                    this.toolbar1.SetSave();
                    Clear();
                }
                else
                {
                    f = new frmHoidap("Thêm Máy chấm công  có lỗi xảy ra!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                    f.ShowDialog();
                    LoadData();
                    this.toolbar1.SetSave();
                    Clear();
                }
            }
            if (flag == 2)
            {
                try
                {
                    f = new frmHoidap("Bạn có muốn sửa Máy chấm công   ?", frmHoidap.CO.hien, frmHoidap.KHONG.hien, frmHoidap.THOAT.an, frmHoidap.HINH.canhbao);
                    if (f.ShowDialog() == DialogResult.Yes)
                    {
                        if (MAYCHAMCONGBUS.Update(AddDuLieu(maychamcong)) == true)
                        {
                            f = new frmHoidap("Sửa Máy chấm công  thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                            f.ShowDialog();
                            LoadData();
                            this.dgvdanhsachmay.ReadOnly = true;
                            this.toolbar1.SetSave();
                            Clear();
                        }
                        else
                        {
                            f = new frmHoidap("Có lỗi khi Sửa Máy chấm công  !", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                            f.ShowDialog();
                            this.toolbar1.SetSave();
                            this.dgvdanhsachmay.ReadOnly = true;
                            Clear();
                        }
                    }
                    else
                    {
                        this.dgvdanhsachmay.ReadOnly = true;
                        this.toolbar1.SetSave();
                        Clear();
                    }
                }
                catch { return; }
            }
            if (flag == 3)
            {
                f = new frmHoidap("Bạn có chắc chắn muốn xóa dữ liệu Máy chấm công  vừa chọn?", frmHoidap.CO.hien, frmHoidap.KHONG.hien, frmHoidap.THOAT.an, frmHoidap.HINH.dauhoi);
                if (f.ShowDialog() == DialogResult.Yes)
                {
                    foreach (DataGridViewRow dr in this.dgvdanhsachmay.Rows)
                    {
                        try
                        {
                            if (dr.Cells[0].Value != null)
                            {
                               maychamcong=new MAYCHAMCONG();
                                maychamcong.MAMCC = dr.Cells["MAMCC"].Value.ToString();
                                MAYCHAMCONGBUS.Delete(maychamcong);
                            }
                        }
                        catch
                        {
                            f = new frmHoidap("Bạn chưa chọn phần Máy chấm công nào vui lòng chọn lại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                            f.ShowDialog();
                        }
                    }
                    f = new frmHoidap("Xóa thành công Máy chấm công !", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                    f.ShowDialog();
                    RemoveCheckbox();
                    this.toolbar1.SetSave();
                    this.dgvdanhsachmay.ReadOnly = true;
                    this.dgvdanhsachmay.AllowUserToAddRows = true;
                }

            }

        }
        void CreateID()
        {
           maychamcong=new MAYCHAMCONG();
           this.txtmamcc.Text = MAYCHAMCONGBUS.GetID();
        }
        private void Clear()
        {
            foreach (Control c in this.Controls)
            {
                if (c is Panel)
                    foreach (Control d in c.Controls)
                        if (d is TextBox)
                            d.Text = "";
            }
        }
        void LoadData()
        {
            try
            {
                Common.HightLightDataGridViewRow(ref this.dgvdanhsachmay, MAYCHAMCONGBUS.SelectAll());
                SetGrid();
                BindCombo();
                this.txtip.Text = Common.GetLocalIpAddress().ToString();
            }
            catch 
            {}
        }
        void SetGrid()
        {

            this.dgvdanhsachmay.Columns["MAMCC"].FillWeight = 200;
            this.dgvdanhsachmay.Columns["TENMAY"].FillWeight = 100;
            this.dgvdanhsachmay.Columns["IDMAY"].FillWeight =80;
            this.dgvdanhsachmay.Columns["IP"].FillWeight = 200;
            this.dgvdanhsachmay.Columns["PORT"].FillWeight = 80;
            this.dgvdanhsachmay.Columns["ISDOMAIN"].FillWeight = 150;
            this.dgvdanhsachmay.Columns["DOMAIN"].FillWeight = 200;
            this.dgvdanhsachmay.Columns["MALM"].Visible = false;
            this.dgvdanhsachmay.Columns["MATD"].Visible = false;
            this.dgvdanhsachmay.Columns["MALKN"].Visible = false;
            this.dgvdanhsachmay.Columns["COMPORT"].FillWeight = 120;
            this.dgvdanhsachmay.Columns["FIMWARE"].FillWeight = 150;
            this.dgvdanhsachmay.Columns["SeriNumber"].FillWeight = 180;
            this.dgvdanhsachmay.Columns["TONGSONHANVIEN"].FillWeight = 180;
            this.dgvdanhsachmay.Columns["SODANGKI"].Visible = false;
            this.dgvdanhsachmay.Columns["TONGSONHANVIEN"].Visible = false;
            this.dgvdanhsachmay.Columns["TONGSOVANTAY"].Visible = false;
            this.dgvdanhsachmay.Columns["TONGSOMATMA"].Visible = false;
            this.dgvdanhsachmay.Columns["PASSWORD"].Visible = false;
        }
        void BindCombo()
        {
            Common.AddComport(ref this.cmbcom);
            this.cmbloaikn.DataSource = KETNOIBUS.SelectAll();
            this.cmbloaikn.DisplayMember = "TENKN";
            this.cmbloaikn.ValueMember="MAKN";
            this.cmbtocdo.DataSource = TOCDOBUS.SelectAll();
            this.cmbtocdo.DisplayMember = "TENTOCDO";
            this.cmbtocdo.ValueMember = "MATD";
            this.cmbloaimay.DataSource = LOAIMAYBUS.SelectAll();
            this.cmbloaimay.DisplayMember = "TENLM";
            this.cmbloaimay.ValueMember = "MALM";
            
        }
        private void InitGrid()
        {
            DgvFilterManager f = new DgvFilterManager();
            f.DataGridView = (DataGridView)this.dgvdanhsachmay;
        }
        void ChooseData(object sender, int e)
        {
            if (flag == 2)
            {
                try
                {
                    this.dgvdanhsachmay.EndEdit();
                    DataGridViewRow dr = this.dgvdanhsachmay.Rows[e];
                    //MAMCC, TENMAY, IDMAY,IP, PORT,  (case ISDOMAIN when 1 then 'USE' when 0 then 'NOT USE' end ) as ISDOMAIN, DOMAIN,
                    //MALM, MATD, MALKN, COMPORT, FIMWARE, SeriNumber, SODANGKI, TONGSONHANVIEN, TONGSOVANTAY, TONGSOMATMA,PASSWORD
                    txtmamcc.Text = dr.Cells["MAMCC"].Value.ToString();
                    txttenmay.Text = dr.Cells["TENMAY"].Value.ToString();
                    txtidmay.Text = dr.Cells["IDMAY"].Value.ToString();
                    this.cmbloaimay.SelectedValue = dr.Cells["MALM"].Value.ToString();
                    if (dr.Cells["MALKN"].Value.ToString() == "LKN110521")//lan
                    {
                        this.cmbloaikn.SelectedIndex = 0;
                        txtip.Text = dr.Cells["IP"].Value.ToString();
                        txtport.Text = dr.Cells["PORT"].Value.ToString();
                        txtpass.Text = dr.Cells["PASSWORD"].Value.ToString();
                    }
                    else if (dr.Cells["MALKN"].Value.ToString() == "LKN1105211")//com
                    {
                        this.cmbloaikn.SelectedIndex = 1;
                        this.cmbtocdo.SelectedValue = dr.Cells["MATD"].Value.ToString();
                        this.cmbcom.SelectedIndex = this.cmbcom.FindString("COM" + dr.Cells["COMPORT"].Value.ToString());
                    }
                    else if (dr.Cells["MALKN"].Value.ToString() == "LKN110604")//usb
                    {
                        this.cmbloaikn.SelectedIndex = 2;
                    }
                    if (dr.Cells["ISDOMAIN"].Value.ToString() == "USE")
                    {
                        cbtenmin.Checked = true;
                    }
                    else
                    {
                        cbtenmin.Checked = false;
                    }
                    txtfimwave.Text = dr.Cells["FIMWARE"].Value.ToString();
                    txttenmien.Text = dr.Cells["DOMAIN"].Value.ToString();
                }
                catch { return; };
            }
        }

        private void Khaibaomaychamcong_Load(object sender, EventArgs e)
        {
            Event();
            SetLink();
        }

        private void cmbloaikn_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbloaikn.SelectedValue.ToString() != "" && this.cmbloaikn.SelectedIndex != -1)
            {
                if (this.cmbloaikn.Text.ToString() == "TCP/IP")
                {
                    txtip.Enabled = true;
                    txtport.Enabled = true;
                    txtpass.Enabled = true;
                }
                else
                {
                    txtip.Enabled = false;
                    txtport.Enabled = false;
                    txtpass.Enabled = false;
                }
                if (this.cmbloaikn.Text.ToString() == "COM")
                {
                    cmbcom.Enabled = true;
                    cmbtocdo.Enabled = true;
                }
                else
                {
                    cmbcom.Enabled = false;
                    cmbtocdo.Enabled = false;
                }
            }
        }
        private void SetLink()
        {
            this.links1.SetValues("Máy", "Khai báo máy chấm công");
        }
        private void cmbloaimay_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetIp();
        }

        private void dgvdanhsachmay_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            ChooseData(sender, e.RowIndex);
        }

        private void dgvdanhsachmay_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
        {
            ChooseData(sender, 0);
        }

        private void dgvdanhsachmay_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            ChooseData(sender, e.RowIndex);
        }

    }
}
